package com.jhgsys.internal.peccancy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.peccancy.entity.PeccancyClause;
import com.jhgsys.internal.peccancy.service.IPeccancyClauseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Date;

/**
 * 违章条款类型 Controller
 *
 * @author admin
 * @date 2024-08-08 13:20:26
 */
@Slf4j
@Validated
@Controller
public class PeccancyClauseController extends BaseController {

    @Autowired
    private IPeccancyClauseService peccancyClauseService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "peccancyClause")
    @RequiresPermissions("peccancyClause:view")
    public String peccancyClauseIndex(Model model){
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        model.addAttribute("tenantList",getTenantList());
        model.addAttribute("currentTenantId", getCurrentTenantId());
        return jumpPage("peccancy/peccancyClause/peccancyClauseList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "peccancyClause/add")
    @RequiresPermissions("peccancyClause:add")
    public String peccancyClauseAdd(){
        return jumpPage("peccancy/peccancyClause/peccancyClauseEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "peccancyClause/update/{id}")
    @RequiresPermissions("peccancyClause:edit")
    public String peccancyClauseUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("peccancyClause", peccancyClauseService.findPeccancyClauseById(id));
        return jumpPage("peccancy/peccancyClause/peccancyClauseEdit");
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-08 13:20:26
     */
    @GetMapping("peccancyClause")
    @ResponseBody
    @RequiresPermissions("peccancyClause:view")
    public FebsResponse getAllPeccancyClauses(PeccancyClause peccancyClause) {
        return new FebsResponse().success().data(peccancyClauseService.findPeccancyClauseRuleList(peccancyClause));
    }

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-08 13:20:26
     */
    @GetMapping("peccancyClause/list")
    @ResponseBody
    @RequiresPermissions("peccancyClause:view")
    public FebsResponse peccancyClauseList(QueryRequest request, PeccancyClause peccancyClause) {
        Map<String, Object> dataTable = getDataTable(this.peccancyClauseService.findPeccancyClausePage(request, peccancyClause));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 PeccancyClause
     * @author admin
     * @date 2024-08-08 13:20:26
     */
    @Log("新增条款类型")
    @PostMapping("peccancyClause")
    @ResponseBody
    @RequiresPermissions("peccancyClause:add")
    public FebsResponse addPeccancyClause(@RequestBody PeccancyClause peccancyClause) throws FebsException {
        checkParams(peccancyClause);
        try {
            peccancyClause.setCreatedBy(getCurrentUser().getUserName());
            this.peccancyClauseService.createPeccancyClause(peccancyClause);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增条款类型失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 逻辑删除 条款类型
     * @author admin
     * @date 2024-08-08 13:20:26
     */
    @Log("逻辑删除条款类型")
    @GetMapping("peccancyClause/delete/{id}")
    @ResponseBody
    @RequiresPermissions("peccancyClause:delete")
    public FebsResponse deletePeccancyClause(@PathVariable("id") String peccancyClauseIds) throws FebsException {
        try {
            PeccancyClause peccancyClause = new PeccancyClause();
            peccancyClause.setDeletedBy(getCurrentUser().getUserName());
            peccancyClause.setDeletedTime(new Date());
            peccancyClause.setDeletedFlg(FebsConstant.INVALID);
            this.peccancyClauseService.deletePeccancyClause(peccancyClause,peccancyClauseIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除条款类型失败";
            log.error(message, e);
            return failure(e,message);
        }
    }


    /**
     * 修改 条款类型
     * @author admin
     * @date 2024-08-08 13:20:26
     */
    @Log("修改条款类型")
    @PostMapping("peccancyClause/update")
    @ResponseBody
    @RequiresPermissions("peccancyClause:edit")
    public FebsResponse updatePeccancyClause(@RequestBody PeccancyClause peccancyClause) throws FebsException {
        checkParams(peccancyClause);
        if(StringUtils.isBlank(peccancyClause.getClauseId())){
            throw new FebsException("条款ID不能为空");
        }
        try {
            peccancyClause.setUpdatedBy(getCurrentUser().getUserName());
            this.peccancyClauseService.updatePeccancyClause(peccancyClause);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改条款类型失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 BaseGoods
     * @author admin
     * @date 2024-08-06 09:41:04
     */
    @Log("修改条款类型状态")
    @PostMapping("peccancyClause/updateStatus")
    @ResponseBody
    @RequiresPermissions("peccancyClause:edit")
    public FebsResponse updateBaseStatus(@RequestBody PeccancyClause peccancyClause) throws FebsException {
        try {
            peccancyClause.setUpdatedBy(getCurrentUser().getUserName());
            this.peccancyClauseService.updatePeccancyClause(peccancyClause);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改条款类型失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    private void checkParams(PeccancyClause peccancyClause) throws FebsException {
        if(StringUtils.isBlank(peccancyClause.getClauseName())){
            throw new FebsException("条款名称不能为空");
        }
        if(StringUtils.isBlank(peccancyClause.getUseScope())){
            throw new FebsException("适用范围不能为空");
        }
        if(StringUtils.isBlank(peccancyClause.getStatus())){
            throw new FebsException("状态不能为空");
        }
        if(StringUtils.isBlank(peccancyClause.getSort())){
            throw new FebsException("排序不能为空");
        }
    }

//    /**
//     * 导出 PeccancyClause的excel
//     * @author admin
//     * @date 2024-08-08 13:20:26
//     */
//    @PostMapping("peccancyClause/excel")
//    @ResponseBody
//    @RequiresPermissions("peccancyClause:view")
//    public void export(QueryRequest queryRequest, PeccancyClause peccancyClause, HttpServletResponse response) throws FebsException {
//        try {
//            List<PeccancyClause> peccancyClauses = this.peccancyClauseService.findPeccancyClausePage(queryRequest, peccancyClause).getRecords();
//            ExcelKit.$Export(PeccancyClause.class, response).downXlsx(peccancyClauses, false);
//        } catch (Exception e) {
//            String message = "导出Excel失败";
//            log.error(message, e);
//            throw new FebsException(message);
//        }
//    }
}
